package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.adapter.CouponsCheckListAdapter;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.MyCouponBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.ICheckCouponListView;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 选择优惠券界面
 */
public class CouponsCheckActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener,XListView.IXListViewListener,ICheckCouponListView{

    @ViewInject(R.id.lv_mygame_list)
    XListView lv_mygame_list;
    @ViewInject(R.id.btn_checkcoupon)
    Button btn_checkcoupon;
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private CouponsCheckListAdapter mCouponsListAdapter;
    private int checkNum; // 记录选中的条目数量
    private MyCouponBean myCouponBean=new MyCouponBean();
    private MyCouponBean myCouponBeanadd;
    private int page=1;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private HashMap<Integer, Boolean> isSelected;
    private String money="0";
    private String couponId="-1";
    private List beSelectedData = new ArrayList();
    @Override
    protected int contentView() {
        return R.layout.activity_coupons_check;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        initToolbar("优惠券");
        getData();
        initEvent();
    }

    private void getData() {
        showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(this,this);
        myOkHttpClient.getMyCoupon(UrlData.BASEURL + UrlData.MYCOUPON, SharePrefencesUtil.getUser_id(this), page + "","1", handler);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        lv_mygame_list.setOnItemClickListener(this);
        btn_checkcoupon.setOnClickListener(this);
        lv_mygame_list.setPullLoadEnable(true);
        lv_mygame_list.setPullRefreshEnable(true);
        lv_mygame_list.setXListViewListener(this);
        lv_mygame_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                getData();
                break;
            case R.id.btn_checkcoupon:
                Intent intent=new Intent();
                intent.putExtra("money",money);
                intent.putExtra("couponId",couponId);
                setResult(-1,intent);
                finish();
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyCouponBean.Data data= (MyCouponBean.Data) parent.getAdapter().getItem(position);
        Log.i("map", data.getPrice().toString());
        //showError(money);
    }

    public void onLoad() {
        lv_mygame_list.stopRefresh();
        lv_mygame_list.stopLoadMore();
        lv_mygame_list.setRefreshTime("");
    }
    @Override
    public void onRefresh() {
        page=1;
        lv_mygame_list.setPullLoadEnable(true);
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }
    void initList(){

        if (myCouponBeanadd.getData() == null || myCouponBeanadd.getData().size() == 0)
            return;
        if (isSelected != null)
            isSelected = null;
        isSelected = new HashMap<Integer, Boolean>();

        for (int i = 0; i < myCouponBeanadd.getData().size(); i++) {
            isSelected.put(i, false);
        }
        // 清除已经选择的项
        if (beSelectedData.size() > 0) {
            beSelectedData.clear();
        }

    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        hideLoading();
                        Log.e("result", result);

                        myCouponBean = gson.fromJson(result, MyCouponBean.class);
                        if (myCouponBeanadd == null)
                        {
                            myCouponBeanadd = myCouponBean;
                        } else {
                            if (myCouponBean.getData() == null) {
                                myCouponBeanadd = myCouponBean;
                            } else {
                                myCouponBeanadd.getData().addAll(myCouponBean.getData());
                            }
                        }
                        initList();
                        mCouponsListAdapter = new CouponsCheckListAdapter(CouponsCheckActivity.this, myCouponBean, beSelectedData, isSelected,CouponsCheckActivity.this);
                        if (myCouponBean.getData().size() == 0 & page == 1)
                        {
                            llnoContent.setVisibility(View.VISIBLE);
                            lv_mygame_list.setVisibility(View.GONE);
                        } else
                        {
                            llnoContent.setVisibility(View.GONE);
                            lv_mygame_list.setVisibility(View.VISIBLE);
                            if (page == 1)
                            {
                                lv_mygame_list.setAdapter(mCouponsListAdapter);
                            } else
                            {
                                mCouponsListAdapter.addData(myCouponBean, beSelectedData, isSelected);

                            }
                            if (myCouponBean.getTotal() <= (myCouponBean.getEach() * page))
                            {
                                lv_mygame_list.setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        hideLoading();
                        showError(result);
                        llRefresh.setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };

    @Override
    public void setMoney(String money,String id) {
        this.money=money;
        this.couponId=id;
    }
}
